package com.bajiao.admin.controller.appVersion;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bajiao.common.api.vo.Result;
import com.bajiao.common.aspect.annotation.AutoLog;
import com.bajiao.common.web.BaseController;
import com.bajiao.entity.app.Version;
import com.bajiao.service.appVersion.VersionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @Description: app版本更新记录表
 * @Author: FanJq
 * @Date: 2024-10-10-12-17
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "app版本更新记录表")
@RestController
@RequestMapping("/app/version")
public class VersionController extends BaseController {
    @Autowired
    private VersionService versionService;

    /**
     * 分页列表查询
     *
     * @param version
     * @return
     */
    @AutoLog(value = "app版本更新记录表-分页列表查询")
    @ApiOperation(value = "app版本更新记录表-分页列表查询", notes = "app版本更新记录表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(Version version) {
        Result<IPage<Version>> result = new Result<IPage<Version>>();
        QueryWrapper<Version> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(version.getName())) {
            queryWrapper.lambda().like(Version::getName, version.getName());
        }
        if (version.getVersion() != null) {
            queryWrapper.lambda().like(Version::getVersion, version.getVersion());
        }
        startPage();
        List<Version> list = versionService.list(queryWrapper);
        return Result.ok(getDataTable(list));
    }

    /**
     * 添加
     *
     * @param version
     * @return
     */
    @AutoLog(value = "app版本更新记录表-添加")
    @ApiOperation(value = "app版本更新记录表-添加", notes = "app版本更新记录表-添加")
    @PostMapping(value = "/add")
    public Result<Version> add(@RequestBody Version version) {
        Result<Version> result = new Result<Version>();
        try {
            versionService.save(version);
            result.success("添加成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    /**
     * 编辑
     *
     * @param version
     * @return
     */
    @AutoLog(value = "app版本更新记录表-编辑")
    @ApiOperation(value = "app版本更新记录表-编辑", notes = "app版本更新记录表-编辑")
    @PutMapping(value = "/edit")
    public Result<Version> edit(@RequestBody Version version) {
        Result<Version> result = new Result<Version>();
        Version versionEntity = versionService.getById(version.getId());
        if (versionEntity == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = versionService.updateById(version);
            //TODO 返回false说明什么？
            if (ok) {
                result.success("修改成功!");
            }
        }

        return result;
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "app版本更新记录表-通过id删除")
    @ApiOperation(value = "app版本更新记录表-通过id删除", notes = "app版本更新记录表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        try {
            versionService.removeById(id);
        } catch (Exception e) {
            log.error("删除失败", e.getMessage());
            return Result.error("删除失败!");
        }
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "app版本更新记录表-批量删除")
    @ApiOperation(value = "app版本更新记录表-批量删除", notes = "app版本更新记录表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<Version> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        Result<Version> result = new Result<Version>();
        if (ids == null || "".equals(ids.trim())) {
            result.error500("参数不识别！");
        } else {
            this.versionService.removeByIds(Arrays.asList(ids.split(",")));
            result.success("删除成功!");
        }
        return result;
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "app版本更新记录表-通过id查询")
    @ApiOperation(value = "app版本更新记录表-通过id查询", notes = "app版本更新记录表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<Version> queryById(@RequestParam(name = "id", required = true) String id) {
        Result<Version> result = new Result<Version>();
        Version version = versionService.getById(id);
        if (version == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(version);
            result.setSuccess(true);
        }
        return result;
    }


}
